package com.zp.self.module.返回xml结构数据;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.zp.self.module.返回xml结构数据.重写序列化方式.Examples;
import org.junit.Test;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author ：by peng.zeng
 * @date ：Created 2021/5/21 15:18
 * @description ：测试构建xml格式，然后打印相关参数
 */
public class MyTestXml2String {

    /**
     * Description: 默认的方式
     * @Author: peng.zeng
     * @DateTime: 2021/5/21 18:07
     * @param
     * @return void
     */
    @Test
    public void testXml(){
        Person person = new Person();
        person.setIdsdad("zhuan2quan");
        person.setName("程序新视界");
        person.setInterest(new String[]{"Java", "Spring Boot", "JVM"});
        XmlMapper xmlMapper = new XmlMapper();
        try {
            String xml = xmlMapper.writeValueAsString(person);
            System.out.println(xml);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }




    /**
     * Description: 重写序列化方式
     * @Author: peng.zeng
     * @DateTime: 2021/5/21 18:07
     * @param args
     * @return void
     */
    public static void main(String[] args) throws Exception {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("Puppy", Boolean.TRUE);
        map.put("Apple", 2);
        map.put("Jet", "Li");
        Examples examples = new Examples();
        examples.setOverlyComplicated("yes");
        examples.setMap(map);

        XmlMapper mapper = new XmlMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);

        ObjectNode rootNode = mapper.createObjectNode();
        rootNode.putPOJO("Examples", examples);

        ObjectNode currentNode = rootNode.putObject("Single");
        currentNode.put("One", 1);

        mapper.writeValue(System.out, rootNode);
    }

}
